package com.honor.games.wg.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.RenderingHints.Key;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.eventbus.EventBus;

@Service
public class DebugFrame extends JFrame {

	private static Logger logger = LoggerFactory.getLogger(DebugFrame.class);

	private JPanel contentPane;
	private JTextField commandField;

	private EventBus eventBus;

	private JButton btnEnterCommand;
	private JTextPane commandTextPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					DebugFrame frame = new DebugFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public DebugFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 583, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		commandField = new JTextField();
		commandField.setText("create unit orc p1 1 1");
		contentPane.add(commandField, BorderLayout.SOUTH);

		commandField.setColumns(10);

		btnEnterCommand = new JButton("Enter Command");
		contentPane.add(btnEnterCommand, BorderLayout.EAST);

		commandTextPane = new JTextPane();
		contentPane.add(commandTextPane, BorderLayout.CENTER);
	}

	public void addEventListeners(EventBus eventbus) {
		this.eventBus = eventbus;

		DebugFrameActionListener debugFrameActionListener = new DebugFrameActionListener(commandField, commandTextPane, eventBus); 		
		btnEnterCommand.addActionListener(debugFrameActionListener);

		commandField.addActionListener(debugFrameActionListener);
	}

	public void appendTextToConsole(Color color, String text) {
		StyledDocument doc = commandTextPane.getStyledDocument();
		StringBuilder builder = new StringBuilder();
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("[dd/MM/yyyy-hh:mm:ss]");
		builder.append(df.format(date));
		builder.append(" ");
		builder.append(text);
		SimpleAttributeSet sas = new SimpleAttributeSet();
		StyleConstants.setForeground(sas, color);
		try {
			doc.insertString(0, builder.toString(), sas);
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}
	}

}
